package com.ossjk.qlh.dishes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.dishes.entity.Dishes;
import com.ossjk.qlh.dishes.service.IDishesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 *
 * 菜品
 * @author liuxi
 * @since 2020-08-12
 */
@Api(tags = "菜品")
@RestController
@RequestMapping("/dishes/dishes")
public class DishesController extends BaseController {

	@Autowired
	private IDishesService iDishesService;

	@ApiOperation(value = "列表")
	@RequiresPermissions("001000")
	@PostMapping("/list")
	public ResponseBean<Page<Dishes>> list(@RequestBody RequestBean requestBean) {
		QueryWrapper<Dishes> queryWrapper = new QueryWrapper<Dishes>();
		String name = (String) requestBean.getParam().get("name");
		Integer type = (Integer) requestBean.getParam().get("type");
		Integer isspicy = (Integer) requestBean.getParam().get("isspicy");
		if (!CommonUtil.isBlank(name)) {
			queryWrapper.like("name", name);
		}
		if (!CommonUtil.isBlank(type)) {
			queryWrapper.like("type", type);
		}
		if (!CommonUtil.isBlank(isspicy)) {
			queryWrapper.like("isspicy", isspicy);
		}
		return responseSuccess(iDishesService.page(requestBean.getPage(),queryWrapper));
	}

	@ApiOperation(value = "去修改")
	@RequiresPermissions("0010000005")
	@PostMapping("/toUpdate")
	public ResponseBean<Dishes> toUpdate(@RequestBody RequestBean requestBean) {
		Dishes dishes = iDishesService.getById((Serializable) requestBean.getParam().get("id"));
		if (!CommonUtil.isBlank(dishes)) {
			return responseSuccess(dishes);
		} else {
			return responseFail();
		}
	}

	@ApiOperation(value = "添加")
	@RequiresPermissions("0010000000")
	@ApiOperationSupport(ignoreParameters = { "id", "crer", "mder", "crtm", "mdtm" })
	@PostMapping("/save")
	public ResponseBean save(@RequestBody RequestBean<Dishes> requestBean) {
		requestBean.getData().setState(new BigDecimal(1));
		requestBean.getData().setFlike("false");
		if (iDishesService.save(requestBean.getData())) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}



	@ApiOperation(value = "修改")
	@RequiresPermissions("0010000005")
	@ApiOperationSupport(ignoreParameters = { "crer", "mder", "crtm", "mdtm" })
	@PostMapping("/update")
	public ResponseBean update(@RequestBody RequestBean<Dishes> requestBean) {
		requestBean.getData().setFlike("false");
		if (iDishesService.updateById(requestBean.getData())) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@ApiOperation(value = "删除")
	@RequiresPermissions("0010000010")
	@PostMapping("/remove")
	public ResponseBean delete(@RequestBody RequestBean requestBean) {
		List ids = (List) requestBean.getParam().get("ids");
		if (iDishesService.removeByIds(ids)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@ApiOperation(value = "主菜列表")
	@PostMapping("/ZClist")
	public ResponseBean<Page<Dishes>> ZClist(@RequestBody RequestBean requestBean) {
		QueryWrapper<Dishes> queryWrapper = new QueryWrapper<Dishes>();
		queryWrapper.eq("type",1);
		return responseSuccess(iDishesService.page(requestBean.getPage(),queryWrapper));
	}

	@ApiOperation(value = "辅菜列表")
	@PostMapping("/FClist")
	public ResponseBean<Page<Dishes>> FClist(@RequestBody RequestBean requestBean) {
		QueryWrapper<Dishes> queryWrapper = new QueryWrapper<Dishes>();
		queryWrapper.eq("type",2);
		return responseSuccess(iDishesService.page(requestBean.getPage(),queryWrapper));
	}

	@ApiOperation(value = "青菜列表")
	@PostMapping("/QClist")
	public ResponseBean<Page<Dishes>> QClist(@RequestBody RequestBean requestBean) {
		QueryWrapper<Dishes> queryWrapper = new QueryWrapper<Dishes>();
		queryWrapper.eq("type",3);
		return responseSuccess(iDishesService.page(requestBean.getPage(),queryWrapper));
	}

	@ApiOperation(value = "主食列表")
	@PostMapping("/ZSlist")
	public ResponseBean<Page<Dishes>> ZSlist(@RequestBody RequestBean requestBean) {
		QueryWrapper<Dishes> queryWrapper = new QueryWrapper<Dishes>();
		queryWrapper.eq("type",4);
		return responseSuccess(iDishesService.page(requestBean.getPage(),queryWrapper));
	}

	@ApiOperation(value = "其他列表")
	@PostMapping("/QTlist")
	public ResponseBean<Page<Dishes>> QTlist(@RequestBody RequestBean requestBean) {
		QueryWrapper<Dishes> queryWrapper = new QueryWrapper<Dishes>();
		queryWrapper.eq("type",5);
		return responseSuccess(iDishesService.page(requestBean.getPage(),queryWrapper));
	}

}
